#include "aewavespectrum.h"
#include "private/aespectrumprivatedefine.h"
#include "common/dataspecificationutils.h"
#include "common/binaryprocesshelper.h"
#include "common/xmldocument.h"
#include "private/spectrumdatafilecommondefine.h"
#include "common/xmlprocesshelper.h"
#include <QDebug>

namespace DataSpecificationGW
{
    class AEWaveSpectrumPrivate
    {
    public:
        AEWaveExtInformationPrivate m_stAEWaveExtInformationPrivate;
        AEWaveDataPrivate m_stAEWaveDataPrivate;
    };

    AEWaveSpectrum::AEWaveSpectrum()
        : Spectrum(),
          m_pAEWaveSpectrumPrivate(new AEWaveSpectrumPrivate)
    {
        setSpectrumTypeCode(SPECTRUM_CODE_AE_WAVE);
    }

    AEWaveSpectrum::~AEWaveSpectrum()
    {
    }

    /************************************************
     * 函数名   : setDataSpecificationVersion
     * 输入参数 :
       const DataSpecificationVersion eDataSpecificationVersion: 数据规范版本号
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置数据规范版本号
     ************************************************/
    void AEWaveSpectrum::setDataSpecificationVersion(const DataSpecificationVersion eDataSpecificationVersion)
    {
        Spectrum::setDataSpecificationVersion(eDataSpecificationVersion);
        if (V_1_1_0_0 == eDataSpecificationVersion)
        {
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipTriggerThreshold();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipOpenTime();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipShutTime();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipMaxIntervalTime();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipGain();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipSyncSource();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipSyncState();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipSyncFrequency();
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.skipDischargeSeverity();
        }
    }

    /************************************************
     * 函数名   : spectrumName
     * 输入参数 : NULL
     * 输出参数 : NULL
     * 返回值   : QString
     * 功能     : 图谱名称
     ************************************************/
    QString AEWaveSpectrum::spectrumName() const
    {
        return STR_FILE_NODE_AE_PULSE;
    }

    /************************************************
     * 函数名   : setAEWaveExtInformation
     * 输入参数 :
       const AEWaveExtInformation& stAEWaveExtInformation: AE相位ExtInformation
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置AE相位ExtInformation
     ************************************************/
    void AEWaveSpectrum::setAEWaveExtInformation(const AEWaveExtInformation& stAEWaveExtInformation)
    {
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit = stAEWaveExtInformation.eAmpUnit;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit = stAEWaveExtInformation.fAmpLowerLimit;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit = stAEWaveExtInformation.fAmpUpperLimit;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType = stAEWaveExtInformation.eAESensorType;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint = stAEWaveExtInformation.iDataPoint;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate = stAEWaveExtInformation.llSampleRate;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability.reserve(8);

        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability.clear();
        for (const quint8& ucDischargeTypeProb : stAEWaveExtInformation.aucPDTypeProbability)
        {
            m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability.push_back(ucDischargeTypeProb);
        }

        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold = stAEWaveExtInformation.fTriggerThreshold;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime = stAEWaveExtInformation.sOpenTime;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime = stAEWaveExtInformation.sShutTime;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime = stAEWaveExtInformation.sMaxIntervalTime;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain = stAEWaveExtInformation.sGain;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource = stAEWaveExtInformation.eSyncSource;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState = stAEWaveExtInformation.ucSyncState;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency = stAEWaveExtInformation.fSyncFrequency;
        m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity = stAEWaveExtInformation.eDischargeSeverity;
    }

    /************************************************
     * 函数名   : setAEWaveData
     * 输入参数 :
       const AEWaveData& stAEWaveData: AE相位Data
     * 输出参数 : NULL
     * 返回值   : void
     * 功能     : 设置AE相位Data
     ************************************************/
    void AEWaveSpectrum::setAEWaveData(const AEWaveData& stAEWaveData)
    {
        m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData = stAEWaveData.qbaAEWaveData;
    }

    /************************************************
     * 函数名   : getAEWaveExtInformation
     * 输入参数 : NULL
     * 输出参数 :
       AEWaveExtInformation& stAEWaveExtInformation: AE相位ExtInformation
     * 返回值   : void
     * 功能     : 获取AE相位ExtInformation
     ************************************************/
    void AEWaveSpectrum::getAEWaveExtInformation(AEWaveExtInformation &stAEWaveExtInformation)
    {
        stAEWaveExtInformation.eAmpUnit = static_cast<AmpUnit>(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit);
        stAEWaveExtInformation.fAmpLowerLimit = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit;
        stAEWaveExtInformation.fAmpUpperLimit = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit;
        stAEWaveExtInformation.eAESensorType = static_cast<AESensorType>(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType);
        stAEWaveExtInformation.iDataPoint = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint;
        stAEWaveExtInformation.llSampleRate = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate;

        std::copy(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability.begin(), m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability.end(), stAEWaveExtInformation.aucPDTypeProbability);

        stAEWaveExtInformation.fTriggerThreshold = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold;
        stAEWaveExtInformation.sOpenTime = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime;
        stAEWaveExtInformation.sShutTime = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime;
        stAEWaveExtInformation.sMaxIntervalTime = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime;
        stAEWaveExtInformation.sGain = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain;
        stAEWaveExtInformation.eSyncSource = static_cast<SyncSource>(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource);
        stAEWaveExtInformation.ucSyncState = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState;
        stAEWaveExtInformation.fSyncFrequency = m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency;
        stAEWaveExtInformation.eDischargeSeverity = static_cast<DischargeSeverity>(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity);
    }

    /************************************************
     * 函数名   : setAEWaveData
     * 输入参数 : NULL
     * 输出参数 :
       AEWaveData& stAEWaveData: AE相位Data
     * 返回值   : void
     * 功能     : 获取AE相位Data
     ************************************************/
    void AEWaveSpectrum::getAEWaveData(AEWaveData& stAEWaveData)
    {
        stAEWaveData.qbaAEWaveData = m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData;
    }

    /************************************************
     * 函数名   : saveBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& out: 输出流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存二进制可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::saveBinarySpectrumExtInfo(QDataStream& out)
    {
        out.device()->seek(SPECTRUM_HEADER_LENGTH);

        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUnit)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpLowerLimit)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUpperLimit)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAESensorType)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDataPoint)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSampleRate)
            out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate;

        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipPDTypeProbability)
        {
            for (const quint8& uPDType : m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability)
            {
                out << uPDType;
            }
        }

        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipTriggerThreshold) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipOpenTime) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipShutTime) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipMaxIntervalTime) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipGain) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncSource) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncState) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncFrequency) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency;
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDischargeSeverity) out << m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity;

        DataSpecificationUtils::saveBinaryReservedData(SPECTRUM_BASE_LENGTH - out.device()->size(), out);

        return true;
    }

    /************************************************
     * 函数名   : saveXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
       QDomElement& element: dom元素
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存XML可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::saveXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj, QDomElement& element)
    {
        xmlDocumentObj.beginElement(element);
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);

        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUnit) xmlDocumentObj.setValue(TEXT_AMP_UNIT, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpLowerLimit) xmlDocumentObj.setValue(TEXT_AMP_MIN, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit, 'f', 1));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUpperLimit) xmlDocumentObj.setValue(TEXT_AMP_MAX, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit, 'f', 1));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAESensorType) xmlDocumentObj.setValue(TEXT_SENSOR_TYPE, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDataPoint) xmlDocumentObj.setValue(TEXT_DATA_POINT, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSampleRate) xmlDocumentObj.setValue(TEXT_SAMPLE_RATE, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipPDTypeProbability) xmlDocumentObj.setValue(TEXT_PD_TYPE_PR, DataSpecificationUtils::dischargeTypeProbabilityVector2QString(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipTriggerThreshold) xmlDocumentObj.setValue(TEXT_TRIGGER_THRESHOLD, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold, 'f', 1));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipOpenTime) xmlDocumentObj.setValue(TEXT_OPEN_TIME, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipShutTime) xmlDocumentObj.setValue(TEXT_SHUT_TIME, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipMaxIntervalTime) xmlDocumentObj.setValue(TEXT_MAX_TIME_INTERVAL, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipGain) xmlDocumentObj.setValue(TEXT_GAIN, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncSource) xmlDocumentObj.setValue(TEXT_SYNC_SOURCE, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncState) xmlDocumentObj.setValue(TEXT_SYNC_STATE, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncFrequency) xmlDocumentObj.setValue(TEXT_SYNC_FREQ, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency, 'f', 1));
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDischargeSeverity) xmlDocumentObj.setValue(TEXT_DISCHARGE_SEVERITY, QString::number(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity));

        return true;
    }

    /************************************************
     * 函数名   : saveJSONSpectrumExtInfo
     * 输入参数 :
       rapidjson::Document::AllocatorType& alloc:
       rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存JSON可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::saveJSONSpectrumExtInfo(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& jsonValue)
    {
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUnit) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit, TEXT_AMP_UNIT, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpLowerLimit) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit, TEXT_AMP_MIN, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUpperLimit) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit, TEXT_AMP_MAX, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAESensorType) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType, TEXT_SENSOR_TYPE, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDataPoint) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint, TEXT_DATA_POINT, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSampleRate) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate, TEXT_SAMPLE_RATE, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipPDTypeProbability) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability, TEXT_PD_TYPE_PR, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipTriggerThreshold) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold, TEXT_TRIGGER_THRESHOLD, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipOpenTime) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime, TEXT_OPEN_TIME, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipShutTime) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime, TEXT_SHUT_TIME, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipMaxIntervalTime) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime, TEXT_MAX_TIME_INTERVAL, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipGain) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain, TEXT_GAIN, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncSource) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource, TEXT_SYNC_SOURCE, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncState) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState, TEXT_SYNC_STATE, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncFrequency) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency, TEXT_SYNC_FREQ, alloc, jsonValue);
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDischargeSeverity) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity, TEXT_DISCHARGE_SEVERITY, alloc, jsonValue);

        return true;
    }

    /************************************************
     * 函数名   : saveBinarySpectrumData
     * 输入参数 :
       QDataStream& out: 输出流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存二进制图谱数据段
     ************************************************/
    bool AEWaveSpectrum::saveBinarySpectrumData(QDataStream& out)
    {
        if (m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint <= 0)
        {
            return false;
        }
        out.device()->seek(SPECTRUM_BASE_LENGTH);

        DataSpecificationUtils::saveBinaryPointerData(out, storageDataType(), m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData.data(), m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint);

        return true;
    }

    /************************************************
     * 函数名   : saveXMLSpectrumData
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
       QDomElement& element: dom元素
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存XML图谱数据段
     ************************************************/
    bool AEWaveSpectrum::saveXMLSpectrumData(XMLDocument& xmlDocumentObj, QDomElement& element)
    {
        xmlDocumentObj.beginElement(element);
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_DATA);

        QString strData = m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData.toBase64();
        xmlDocumentObj.setValue(STR_SPECTRUM_NODE_DATA, strData);

        return true;
    }

    /************************************************
     * 函数名   : saveJSONSpectrumData
     * 输入参数 :
       rapidjson::Document::AllocatorType& alloc:
       rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 保存JSON图谱数据段
     ************************************************/
    bool AEWaveSpectrum::saveJSONSpectrumData(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& jsonValue)
    {
        if (!m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.bSkipAEWaveSpectrumData) saveJSONField(m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData, STR_SPECTRUM_NODE_DATA, alloc, jsonValue);
        return true;
    }

    /************************************************
     * 函数名   : parseBinarySpectrumExtInfo
     * 输入参数 :
       QDataStream& in: 输入流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析二进制可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::parseBinarySpectrumExtInfo(QDataStream& in)
    {
        if (!in.device()->seek(SPECTRUM_HEADER_LENGTH))
        {
            qDebug() << "AEWaveSpectrum::parseBinarySpectrumExtInfo failed";
            return false;
        }

        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUnit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUnit);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpLowerLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpLowerLimit);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAmpUpperLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUpperLimit);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipAESensorType, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAESensorType);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDataPoint, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDataPoint);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSampleRate, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSampleRate);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipPDTypeProbability, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability, 8, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasPDTypeProbability);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipTriggerThreshold, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasTriggerThreshold);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipOpenTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasOpenTime);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipShutTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasShutTime);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipMaxIntervalTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasMaxIntervalTime);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipGain, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasGain);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncSource, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncSource);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncState, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncState);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipSyncFrequency, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncFrequency);
        parseBinaryField(in, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bSkipDischargeSeverity, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDischargeSeverity);

        return in.status() == QDataStream::Ok;
    }

    /************************************************
     * 函数名   : parseXMLSpectrumExtInfo
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析XML可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::parseXMLSpectrumExtInfo(XMLDocument& xmlDocumentObj)
    {
        xmlDocumentObj.beginElement(spectrumName());
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_EXTINFORMATION);

        parseXMLField(xmlDocumentObj, TEXT_AMP_UNIT, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUnit);
        parseXMLField(xmlDocumentObj, TEXT_AMP_MIN, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpLowerLimit);
        parseXMLField(xmlDocumentObj, TEXT_AMP_MAX, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUpperLimit);
        parseXMLField(xmlDocumentObj, TEXT_SENSOR_TYPE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAESensorType);
        parseXMLField(xmlDocumentObj, TEXT_DATA_POINT, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDataPoint);
        parseXMLField(xmlDocumentObj, TEXT_SAMPLE_RATE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSampleRate);
        parseXMLField(xmlDocumentObj, TEXT_PD_TYPE_PR, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasPDTypeProbability);
        parseXMLField(xmlDocumentObj, TEXT_TRIGGER_THRESHOLD, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasTriggerThreshold);
        parseXMLField(xmlDocumentObj, TEXT_OPEN_TIME, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasOpenTime);
        parseXMLField(xmlDocumentObj, TEXT_SHUT_TIME, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasShutTime);
        parseXMLField(xmlDocumentObj, TEXT_MAX_TIME_INTERVAL, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasMaxIntervalTime);
        parseXMLField(xmlDocumentObj, TEXT_GAIN, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasGain);
        parseXMLField(xmlDocumentObj, TEXT_SYNC_SOURCE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncSource);
        parseXMLField(xmlDocumentObj, TEXT_SYNC_STATE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncState);
        parseXMLField(xmlDocumentObj, TEXT_SYNC_FREQ, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncFrequency);
        parseXMLField(xmlDocumentObj, TEXT_DISCHARGE_SEVERITY, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDischargeSeverity);

        xmlDocumentObj.endElement();
        xmlDocumentObj.endElement();

        return true;
    }

    /************************************************
     * 函数名   : parseJSONSpectrumExtInfo
     * 输入参数 :
       const rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析JSON可扩展信息段
     ************************************************/
    bool AEWaveSpectrum::parseJSONSpectrumExtInfo(const rapidjson::Value& jsonValue)
    {
        parseJSONField(jsonValue, TEXT_AMP_UNIT, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAmpUnit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUnit);
        parseJSONField(jsonValue, TEXT_AMP_MIN, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpLowerLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpLowerLimit);
        parseJSONField(jsonValue, TEXT_AMP_MAX, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fAmpUpperLimit, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAmpUpperLimit);
        parseJSONField(jsonValue, TEXT_SENSOR_TYPE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucAESensorType, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasAESensorType);
        parseJSONField(jsonValue, TEXT_DATA_POINT, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDataPoint);
        parseJSONField(jsonValue, TEXT_SAMPLE_RATE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.llSampleRate, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSampleRate);
        parseJSONField(jsonValue, TEXT_PD_TYPE_PR, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.vecPDTypeProbability, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasPDTypeProbability);
        parseJSONField(jsonValue, TEXT_TRIGGER_THRESHOLD, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fTriggerThreshold, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasTriggerThreshold);
        parseJSONField(jsonValue, TEXT_OPEN_TIME, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sOpenTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasOpenTime);
        parseJSONField(jsonValue, TEXT_SHUT_TIME, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sShutTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasShutTime);
        parseJSONField(jsonValue, TEXT_MAX_TIME_INTERVAL, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sMaxIntervalTime, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasMaxIntervalTime);
        parseJSONField(jsonValue, TEXT_GAIN, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.sGain, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasGain);
        parseJSONField(jsonValue, TEXT_SYNC_SOURCE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncSource, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncSource);
        parseJSONField(jsonValue, TEXT_SYNC_STATE, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucSyncState, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncState);
        parseJSONField(jsonValue, TEXT_SYNC_FREQ, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.fSyncFrequency, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasSyncFrequency);
        parseJSONField(jsonValue, TEXT_DISCHARGE_SEVERITY, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.ucDischargeSeverity, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.bHasDischargeSeverity);

        return true;
    }

    /************************************************
     * 函数名   : parseBinarySpectrumData
     * 输入参数 :
       QDataStream& in: 输入流
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析二进制图谱数据段
     ************************************************/
    bool AEWaveSpectrum::parseBinarySpectrumData(QDataStream& in)
    {
        if (!in.device()->seek(SPECTRUM_BASE_LENGTH))
        {
            qDebug() << "AEWaveSpectrum::parseBinarySpectrumData failed";
            return false;
        }

        DataSpecificationUtils::parseBinaryPointerData(in, storageDataType(), m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData, m_pAEWaveSpectrumPrivate->m_stAEWaveExtInformationPrivate.iDataPoint);

        return in.status() == QDataStream::Ok;
    }

    /************************************************
     * 函数名   : parseXMLSpectrumData
     * 输入参数 :
       XMLDocument& xmlDocumentObj: xml文档对象
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析XML图谱数据段
     ************************************************/
    bool AEWaveSpectrum::parseXMLSpectrumData(XMLDocument& xmlDocumentObj)
    {
        xmlDocumentObj.beginElement(spectrumName());
        xmlDocumentObj.beginElement(STR_SPECTRUM_NODE_DATA);

        m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData = QByteArray::fromBase64(xmlDocumentObj.value(STR_SPECTRUM_NODE_DATA).toLatin1());

        xmlDocumentObj.endElement();
        xmlDocumentObj.endElement();

        return true;
    }

    /************************************************
     * 函数名   : parseJSONSpectrumData
     * 输入参数 :
       const rapidjson::Value& jsonValue: json数据
     * 输出参数 : NULL
     * 返回值   : bool
     * 功能     : 解析JSON图谱数据段
     ************************************************/
    bool AEWaveSpectrum::parseJSONSpectrumData(const rapidjson::Value& jsonValue)
    {
        parseJSONField(jsonValue, STR_SPECTRUM_NODE_DATA, m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.qbaAEWaveData, m_pAEWaveSpectrumPrivate->m_stAEWaveDataPrivate.bHasAEWaveSpectrumData);

        return true;
    }

}
